﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MAT.Model;
using System.Windows.Forms;

namespace MAT.Controllers
{
    class ctrManageEmployee
    {
        private frmManageEmployee _frmManageEmployee = null;

        public frmManageEmployee FrmManageEmployee
        {
            get { return _frmManageEmployee; }
            set { _frmManageEmployee = value; }
        }

        MAT_Context context = MATContext.getContext();

        private bool check()
        {
            if (_frmManageEmployee.txtName.Text == "")
            {
                MessageBox.Show("Name is empty");
                _frmManageEmployee.txtName.Focus();
                return false;
            }
            if (_frmManageEmployee.txtAddress.Text == "")
            {
                MessageBox.Show("Address is empty");
                _frmManageEmployee.txtAddress.Focus();
                return false;
            }
            if (_frmManageEmployee.txtPhone.Text == "")
            {
                MessageBox.Show("Phone is empty");
                _frmManageEmployee.txtPhone.Focus();
                return false;
            }
            if (_frmManageEmployee.txtEmail.Text == "")
            {
                MessageBox.Show("Email is empty");
                _frmManageEmployee.txtEmail.Focus();
                return false;
            }
            if (_frmManageEmployee.txtRoom.Text == "")
            {
                MessageBox.Show("Room is empty");
                _frmManageEmployee.txtRoom.Focus();
                return false;
            }
            else
                return true;
        }

        public void frmManageUser_Load()
        {
            _frmManageEmployee.cmbEmployeeName.DataSource = context.OWNERS.ToList();
            _frmManageEmployee.cmbEmployeeName.DisplayMember = "EMPLOYEE_NAME";
            _frmManageEmployee.cmbEmployeeName.ValueMember = "EMPLOYEE_ID";
            _frmManageEmployee.txtEmployeeID.Text = _frmManageEmployee.cmbEmployeeName.SelectedValue.ToString();
            _frmManageEmployee.cmbManage.DisplayMember = "EMPLOYEE_NAME";
            _frmManageEmployee.cmbManage.ValueMember = "EMPLOYEE_ID";
            List<LOG_IN> logins = context.LOG_IN.Where(log => log.PRIORITY != 3).ToList();
            List<OWNER> owners = new List<OWNER>();
            foreach (LOG_IN login in logins)
                owners.Add(login.OWNER);
            _frmManageEmployee.cmbManage.DataSource = owners.ToList();
        }

        public void btnAdd_Click()
        {
            if (_frmManageEmployee.btnAdd.Text == "Add")
            {
                if (check())
                {
                    OWNER owner = new OWNER();
                    owner.EMPLOYEE_ID = _frmManageEmployee.txtEmployeeID.Text;
                    owner.EMPLOYEE_NAME = _frmManageEmployee.txtName.Text;
                    owner.EMP_ADDRESS = _frmManageEmployee.txtAddress.Text;
                    owner.PHONE_NUM = _frmManageEmployee.txtPhone.Text;
                    owner.EMAIL = _frmManageEmployee.txtEmail.Text;
                    owner.MANAGE = _frmManageEmployee.cmbManage.SelectedValue.ToString();
                    owner.ROOM_ID = context.ROOMs.FirstOrDefault(ro => ro.ROOM_NAME == _frmManageEmployee.txtRoom.Text).ROOM_ID;
                    try
                    {
                        context.OWNERS.AddObject(owner);
                        context.SaveChanges();
                        MessageBox.Show("SUCCESSFUL!");
                        frmManageUser_Load();
                    }
                    catch { }

                    _frmManageEmployee.txtEmployeeID.Visible = false;
                    _frmManageEmployee.btnAdd.Text = "New";
                    _frmManageEmployee.btnClose.Text = "Close";
                }
            }
            if (_frmManageEmployee.btnAdd.Text == "New")
            {
                _frmManageEmployee.txtEmployeeID.Visible = true;
                _frmManageEmployee.btnAdd.Text = "Add";
                _frmManageEmployee.btnClose.Text = "Cancel";
                _frmManageEmployee.btnReset.Enabled = true;
                return;
            }
        }

        public void btnDelete_Click()
        {
            OWNER owner = context.OWNERS.FirstOrDefault(own => own.EMPLOYEE_ID == _frmManageEmployee.txtEmployeeID.Text);
            if (owner != null)
            {
                try
                {
                    context.OWNERS.DeleteObject(owner);
                    context.SaveChanges();
                    MessageBox.Show("SUCCESSFUL!");
                    frmManageUser_Load();
                }
                catch { }
            }
            else
                MessageBox.Show("Not Found!");
        }

        public void btnEdit_Click()
        {
            OWNER owner = context.OWNERS.FirstOrDefault(own => own.EMPLOYEE_ID == _frmManageEmployee.txtEmployeeID.Text);
            if (owner != null)
            {
                try
                {
                    owner.EMPLOYEE_NAME = _frmManageEmployee.txtName.Text;
                    owner.EMP_ADDRESS = _frmManageEmployee.txtAddress.Text;
                    owner.PHONE_NUM = _frmManageEmployee.txtPhone.Text;
                    owner.EMAIL = _frmManageEmployee.txtEmail.Text;
                    owner.MANAGE = _frmManageEmployee.cmbManage.SelectedValue.ToString();
                    owner.ROOM_ID = context.ROOMs.FirstOrDefault(ro => ro.ROOM_NAME == _frmManageEmployee.txtRoom.Text).ROOM_ID;
                    context.SaveChanges();
                    MessageBox.Show("SUCCESSFUL!");
                    frmManageUser_Load();
                }
                catch { }
            }
            else
                MessageBox.Show("Not Found!");
        }

        public void btnReset_Click()
        {
            _frmManageEmployee.txtName.Text = "";
            _frmManageEmployee.txtAddress.Text = "";
            _frmManageEmployee.txtPhone.Text = "";
            _frmManageEmployee.txtEmail.Text = "";
            _frmManageEmployee.txtRoom.Text = "";
            _frmManageEmployee.cmbManage.Text = "";
        }

        public void btnClose_Click()
        {
            if (_frmManageEmployee.btnClose.Text == "Close")
            {
                _frmManageEmployee.Close();
                MAT.Controllers.ctrMain._frmMain.pnlMain.Visible = false;
            }
            if (_frmManageEmployee.btnClose.Text == "Cancel")
            {
                _frmManageEmployee.txtEmployeeID.Visible = false;
                _frmManageEmployee.btnAdd.Text = "New";
                _frmManageEmployee.btnClose.Text = "Close";
            }
        }

        public void cmbEmployeeName_SelectedIndexChanged()
        {
            string a = _frmManageEmployee.cmbEmployeeName.SelectedValue.ToString();
            _frmManageEmployee.txtEmployeeID.Text = a;
            OWNER owner = context.OWNERS.FirstOrDefault(own => own.EMPLOYEE_ID == a);
            if (owner != null)
            {
                _frmManageEmployee.txtName.Text = owner.EMPLOYEE_NAME;
                _frmManageEmployee.txtAddress.Text = owner.EMP_ADDRESS;
                _frmManageEmployee.txtPhone.Text = owner.PHONE_NUM;
                _frmManageEmployee.txtEmail.Text = owner.EMAIL;
                _frmManageEmployee.cmbManage.SelectedValue = owner.MANAGE;
            }
            else
            {
                _frmManageEmployee.txtName.Text = "";
                _frmManageEmployee.txtAddress.Text = "";
                _frmManageEmployee.txtPhone.Text = "";
                _frmManageEmployee.txtEmail.Text = "";
                _frmManageEmployee.cmbManage.SelectedValue = "";
            }
        }

        public void cmbManage_SelectedIndexChanged()
        {
            try
            {
                string a = _frmManageEmployee.cmbManage.SelectedValue.ToString();
                string roomID = context.OWNERS.FirstOrDefault(own => own.EMPLOYEE_ID == a).ROOM_ID;
                _frmManageEmployee.txtRoom.Text = context.ROOMs.FirstOrDefault(ro => ro.ROOM_ID == roomID).ROOM_NAME;
            }
            catch { }
        }
    }
}

